package com.school.service.impl;

import com.school.dao.RoleDao;
import com.school.dao.impl.RoleDaoImpl;
import com.school.model.Role;
import com.school.service.RoleService;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 复杂功能的包装，对数据单元操作进行组装实现复杂功能
 */
public class RoleServiceImpl implements RoleService {
    /**
     * 创建数据库访问对象实力
     */
    private RoleDao roleDao = new RoleDaoImpl();

    @Override
    public Role insert(Role role) {
        return roleDao.insert(role);
    }

    @Override
    public List<Role> insert(List<Role> students) {
        return roleDao.insert(students);
    }

    @Override
    public void delete(Integer id) {
        roleDao.delete(id);
    }

    @Override
    public void delete(Collection<Integer> ids) {
        roleDao.delete(ids);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public void update(List<Role> students) {
        roleDao.update(students);
    }

    @Override
    public int count() {
        return roleDao.count();
    }

    @Override
    public Role findOneById(Integer id) {
        return roleDao.findOneById(id);
    }

    @Override
    public List<Role> findPage(Role role, Integer page, Integer size) {
        // page : 当前页数 5 ，10    0，1，2，3，4
        int offset = (page - 1) * size;
        return roleDao.findPage(role, offset, size);
    }

    @Override
    public void deleteRolesByUserId(Integer userId) {
        roleDao.deleteRolesByUserId(userId);
    }

    @Override
    public void insertUserRolesByUserId(Integer userId, List<Integer> roleIds) {
        roleDao.insertUserRolesByUserId(userId,Set.copyOf(roleIds));
    }

    @Override
    public void updateUserRoles(Integer userId, Set<Integer> roleIds) {
        // 1、先删除
        roleDao.deleteRolesByUserId(userId);
        // 2、插入
        roleDao.insertUserRolesByUserId(userId,roleIds);
    }
}
